<template>
  <div class="login-page">
    <div class="login-container">
      <div class="register-link">
        <span>新用户？</span>
        <a @click="showRegistration = true">立即注册</a>
      </div>
      <h1>用户登录</h1>
      <form v-if="!showRegistration" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入用户名"
            required
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入密码"
            required
          />
        </div>
        <button type="submit">登录</button>
      </form>
      <form v-else @submit.prevent="handleRegistration">
        <div class="form-group">
          <label for="reg-username">用户名</label>
          <input
            type="text"
            id="reg-username"
            v-model="regUsername"
            placeholder="请输入用户名"
            required
          />
        </div>
        <div class="form-group">
          <label for="reg-password">密码</label>
          <input
            type="password"
            id="reg-password"
            v-model="regPassword"
            placeholder="请输入密码"
            required
          />
        </div>
        <button type="submit">注册</button>
        <button type="button" @click="showRegistration = false">返回登录</button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const username = ref('');
const password = ref('');
const regUsername = ref('');
const regPassword = ref('');
const showRegistration = ref(false);

const handleLogin = async () => {
  try {
    const response = await axios.post('http://127.0.0.1:5000/api/login', {
      username: username.value,
      password: password.value
    });

    if (response.data.success) {
      console.log('登录成功');
      router.push({ name: 'Invoice' });
    } else {
      console.log('登录失败');
      alert('用户名或密码错误');
    }
  } catch (error) {
    console.error('登录请求失败', error);
    alert('登录请求失败，请稍后再试');
  }
};

const handleRegistration = async () => {
  try {
    const response = await axios.post('http://127.0.0.1:5000/api/register', {
      username: regUsername.value,
      password: regPassword.value
    });

    if (response.data.success) {
      console.log('注册成功');
      alert('注册成功，请登录');
      showRegistration.value = false;
    } else {
      console.log('注册失败');
      alert('注册失败，请重试');
    }
  } catch (error) {
    console.error('注册请求失败', error);
    alert('注册请求失败，请稍后再试');
  }
};
</script>

<style scoped>
.login-page {
  width: 98vw;
  height: 97vh;
  background: url('./src/assets/Login_background.png') no-repeat center center/cover;
  display: flex;
  align-items: center;
}

.login-container {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 400px;
  margin-left: 50px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  box-sizing: border-box;
}

button {
  width: 100%;
  padding: 14px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
  margin-bottom: 10px;
}

button:hover {
  background-color: #0056b3;
}

.register-link {
  margin-bottom: 20px;
}

.register-link a {
  color: #007bff;
  text-decoration: none;
  margin-left: 5px;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>
    